<?php

class StretchHelper extends Helper {

    private $_field;
    private $rule;

    public function __construct() {
        $this->_field = array('sr_id', 'sr_oid', 'sr_cause', 'sr_remark', 'sr_uname', 'sr_uid', 'sr_time', 'sr_result', 'sr_idea', 'sr_deal_uname', 'sr_deal_uid', 'sr_deal_time');
        $this->rule = array(
            'sr_cause' => array('require', '通融原因不能为空')
        );
        
    }
    /**
     * 通融申请列表
     * @param type $where
     * @param type $page
     * @param type $limit
     * @param type $param
     * @param type $member
     * @return type
     */
    public function signList($where, $page = 1, $limit = PAGE_SIZE, $param = array()) {
        $model = $this->LoadModel('Public');
        $data['count'] = $model->getRowCount('stretch_rules', $where);
        if (!empty($data['count'])) {
            $data['all'] = $model->getList('stretch_rules', $this->_field, $where, array($page, $limit), NULL, array('sr_id DESC'));
            Pagination::SetUrl($Param);
            $data['page'] = Pagination::GetHtml($limit, $page, $data['count']);
            $data['pagetotal'] = ceil($data['count'] / $limit);
        }
        return $data;
    }
    
    /**
     * 单条数据
     * @param type $where
     * @return type
     */
    public function signRow($where){
        if(is_numeric($where)){
            $where = array('`sr_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getRow('stretch_rules', $this->_field, $where, null, array('sr_id DESC'));
        return $data;
    }
    
    /**
     * 数据保存
     * @param type $data
     * @param type $where
     * @return type
     */
    public function signSave($data, $where = '', $check = true){
        if(is_numeric($where)){
            $where = array('`sr_id` = ?' => $where);
        }
        $validator = Validator::checkRule($data, $this->rule);
        if(!$validator['pass'] && $check){
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $model = $this->LoadModel('Public');
        if(empty($where)){
            $id = $model->save('stretch_rules', $data);
            if($id){
                return array('success' => true, 'msg' => $id);
            }else{
                return array('success' => false, 'msg' => '保存失败');
            }
        }else{
            $model->update('stretch_rules', $data, $where);
            return array('success' => true);
        }
    }
    /**
     * 审核处理
     * @param type $oid
     * @return type
     */
    public function signDetail($sr_id, $params){
        if(empty($sr_id)){
            return array('success' => false, 'msg' => '参数错误');
        }
        $sr_row = $this->signRow($sr_id);        
        $model = $this->LoadModel('Public');
        $order_row = $model->getOneRow('order', '*', array('`o_id` = ?' => $sr_row['sr_oid']));
        if($order_row['o_status'] != '退回') {
            return array('success' => false, 'msg' => '当前订单状态不允许操作');
        }
        /*
        switch ($order_row['o_quit_status']){
            case '初审提交':
                $status = '初审通过';
                break;
            case '初审通过':
                $status = '终审提交';
                break;
            case '终审提交':
                $status = '终审通过';
                break;
        }*
         * 
         */
        //$status = '终审通过';
        $status = $order_row['o_quit_status'];
        $order_up = array('o_status' => $status);
        if($order_row['o_trial_status'] == '未通过'){
            $order_up['o_trial_status'] ='未审核';
       }
        if($order_row['o_layer_status'] == '未通过'){
            $order_up['o_layer_status'] ='未审核';
            $order_up['o_layer_aucode'] ='';
        }
        if($order_row['o_review_status'] == '未通过'){
            $order_up['o_review_status'] = '未复核';
        }
        $model->beginTransaction();
        //file_put_contents('D:dd12.txt',var_export($order_up,true));
        try{
            if($params['sr_result'] == '通过'){
                //$model->update('order', array('o_status' => $status), array('`o_id` = ?' => $sr_row['sr_oid']));
                $model->update('order', $order_up, array('`o_id` = ?' => $sr_row['sr_oid']));
            }
            $model->update('stretch_rules', $params, array('`sr_id` = ?' => $sr_id));
            $model->commit();
            return array('success' => true, 'msg' => '处理成功');
        } catch (Exception $ex) {
            $model->rollBack();
            return array('success' => false, 'msg' => '出现异常'. $ex->getMessage());
        }
    }
    /**
     * 根据单号进行判断是否能新增申请通融
     * @param type $oid
     * @return boolean
     */
    public function signJudge($oid){
        $model = $this->LoadModel('Public');
        $order_row = $model->getOneRow('order', array('o_status'), array('`o_id` = ?' => $oid));
        if($order_row['o_status'] != '退回'){
            return false;
        }
        $sc_all = $model->getList('stretch_rules', '*', array('`sr_oid` = ?' => $oid, '`sr_result` != ?' => 'del'));
        if(count($sc_all) >= 2){
            return false;
        }else{
            foreach($sc_all as $k => $v){
                if($v['sr_result'] == '申请中'){
                    return false;
                }
            }
        }
        return true;
    }
    
    
}